package com.diudiutechnology.www.yadiudiu.yadiudiu.house;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.diudiutechnology.www.yadiudiu.util.utils.CustomLayout;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.view.MyScrollView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.house.MyActivity.HousingDetailsActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.house.MyActivity.RentDetailsActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.LazyFragment;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.SearchActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.BannersAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.house.MyAdapter.BuyHouseAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.house.MyAdapter.HousePurchaseFragmentAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.BuyACarFragmentModel;
import com.diudiutechnology.www.yadiudiu.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/15.
 * 租房买房 主题板块
 */

public class HousePurchaseFragment extends LazyFragment implements View.OnClickListener, MyScrollView.Callbacks {
    private Context context;

    ListView house_list;
    private List<BuyACarFragmentModel> houselist;

    private LinearLayout search_ly;

    private CustomLayout customLayout;
    private ViewPager ad_pager;
    private LinearLayout ad_layer; // 圈圈 布局
    private final int HOME_AD_RESULT = 1;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // 广告
                case HOME_AD_RESULT:
                    ad_pager.setCurrentItem(ad_pager.getCurrentItem() + 1,
                            true);
                    break;
            }
        }

        ;
    };
    RelativeLayout rent_a_house;//租房
    ImageView rent_a_house_img;
    HousePurchaseFragmentAdapter adapter;//租房
    MyScrollView scroll_view;

    RelativeLayout to_buya_house;//买房
    ImageView to_buya_house_img;
    BuyHouseAdapter buyHouseAdapter;//买房
    List<Integer> listint = new ArrayList<>();//买房分类
    private LinearLayout house_layout;
    LinearLayout fixed_position;//固定house_layout位置
    int scroll_viewpos = 0;//点击按钮MyScrollView回到坐标 0，0
    int pos = 0;

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.fragment_house_purchase);
        initView();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
    }

    private void initView() {
        scroll_view = (MyScrollView) findViewById(R.id.scroll_view);
        house_layout = (LinearLayout) findViewById(R.id.house_layout);
        fixed_position = (LinearLayout) findViewById(R.id.fixed_position);
        customLayout = (CustomLayout) findViewById(R.id.customLayout);
        ad_pager = (ViewPager) findViewById(R.id.ad_pager);
        ad_layer = (LinearLayout) findViewById(R.id.ad_layer);
        house_list = (ListView) findViewById(R.id.house_list);
        search_ly = (LinearLayout) findViewById(R.id.search_ly);

        to_buya_house= (RelativeLayout) findViewById(R.id.to_buya_house);
        to_buya_house_img= (ImageView) findViewById(R.id.to_buya_house_img);

        rent_a_house= (RelativeLayout) findViewById(R.id.rent_a_house);
        rent_a_house_img= (ImageView) findViewById(R.id.rent_a_house_img);

        scroll_view.setCallbacks(this);

        scroll_view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (scroll_viewpos == 1) {
                    scroll_view.scrollTo(0, 0);
                    scroll_viewpos = 0;
                }
                onScrollChanged(scroll_view.getScrollY());
            }
        });
        search_ly.setOnClickListener(this);
        to_buya_house.setOnClickListener(this);
        rent_a_house.setOnClickListener(this);
        List<String> getinforresult = new ArrayList<>();
        getinforresult.add("1");
        getinforresult.add("1");
        getinforresult.add("1");
        getadvertising(getinforresult.size(), getinforresult);


        processadapter();
    }
//初始化adapter
    public void processadapter() {
        if (pos == 0) {
                buyHouseAdapter = new BuyHouseAdapter(context);
                house_list.setAdapter(buyHouseAdapter);
        } else {
                adapter = new HousePurchaseFragmentAdapter(context);
                house_list.setAdapter(adapter);
        }
        initData();
    }

    //scrollview初始位置
    public void processscrollTo() {
        if (scroll_view != null) {
            scroll_viewpos = 1;
        }

    }

    //数据
    private void initData() {
        houselist = new ArrayList<>();
        if (pos == 0) {
            houselist.clear();
            listint.clear();
            int f=0;
            BuyACarFragmentModel model;
            for (int i = 0; i < 5; i++) {
                if ((i/2)==0){
                    listint.add(i);
                    f=i;
                }else {
                    listint.add(f);
                }
                 model = new BuyACarFragmentModel();
                model.setName("渝北 万科朗润园 精装单间 家电齐全 拎包入住");
                houselist.add(model);
            }
            buyHouseAdapter.addItem(houselist, listint);
        } else {
            houselist.clear();
            BuyACarFragmentModel model = new BuyACarFragmentModel();
            model.setName("渝北 万科朗润园 精装单间 家电齐全 拎包入住");
            model.setLet("农业园区 万科朗润园");
            model.setMoney1("1,000元/月");
            model.setDistance("0.62km");
            model.setDate("1室1厅  整租");
            houselist.add(model);

            model = new BuyACarFragmentModel();
            model.setName("回兴轻轨站 伴山名都电梯朝内廷 拎 包入住 有图片 友情价出租");
            model.setLet("两路 伴山名都");
            model.setMoney1("950元/月");
            model.setDistance("1.12km");
            model.setDate("1室1厅  整租");
            houselist.add(model);

            model = new BuyACarFragmentModel();
            model.setName("急急急急急急！业主因工作原因，急 租此房！");
            model.setLet("玫瑰城 重庆国际家纺城绣色");
            model.setMoney1("800元/月");
            model.setDistance("1.62km");
            model.setDate("2室1厅  合租");
            houselist.add(model);

            model = new BuyACarFragmentModel();
            model.setName("渝北 万科朗润园 精装单间 家电齐全 拎包入住");
            model.setLet("农业园区 万科朗润园");
            model.setMoney1("1,000元/月");
            model.setDistance("1.12km");
            model.setDate("1室1厅  整租");
            houselist.add(model);

            model.setName("渝北 万科朗润园 精装单间 家电齐全 拎包入住");
            model.setLet("农业园区 万科朗润园");
            model.setMoney1("950元/月");
            model.setDistance("1.12km");
            model.setDate("1室1厅  合租");
            houselist.add(model);
            adapter.addItem(houselist, 1);

        }
        house_list.setOnItemClickListener(itemClick);
    }

    AdapterView.OnItemClickListener itemClick = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            if (pos==0){
                startActivity(new Intent(context, HousingDetailsActivity.class));
            }else {
                startActivity(new Intent(context, RentDetailsActivity.class));
            }

        }
    };

    //轮播广告
    public void getadvertising(final int size, final List<String> getinforresult) {
        ad_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Sundry.refreshPoint(ad_layer, position % size, 0);
                if (mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
                mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if (ViewPager.SCROLL_STATE_DRAGGING == arg0
                        && mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
            }
        });
        BannersAdapter adapter = new BannersAdapter(context, getinforresult, 1);
        ad_pager.setAdapter(adapter);
        Sundry.initPointsLayout(ad_layer, context, size, 0);
        ad_pager.setCurrentItem(size * 1000, false);
        // 自动轮播线程
        mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search_ly:
                startActivity(new Intent(context, SearchActivity.class));
                break;
            case R.id.to_buya_house:
                to_buya_house_img.setVisibility(View.VISIBLE);
                rent_a_house_img.setVisibility(View.GONE);
                pos=0;
                processadapter();
                scroll_viewpos = 1;
                break;
            case R.id.rent_a_house:
                rent_a_house_img.setVisibility(View.VISIBLE);
                to_buya_house_img.setVisibility(View.GONE);
                pos=1;
                processadapter();
                scroll_viewpos = 1;
                break;
        }
    }

    @Override
    public void onScrollChanged(int scrollY) {
        house_layout.setTranslationY(Math.max(fixed_position.getTop(), scrollY));
    }

    @Override
    public void onDownMotionEvent() {
        System.out.println("scroll_viewposonDownMotionEvent");
    }

    @Override
    public void onUpOrCancelMotionEvent() {
        System.out.println("scroll_viewposonUpOrCancelMotionEvent");
    }
}
